﻿namespace com.oakwoodsys.ConsultantCare
{
    using System.Collections.Generic;

    using com.oakwoodsys.ConsultantCareDao;

    using Microsoft.SharePoint;

    /// <summary>
    /// List Item Events
    /// </summary>
    public class RecruiterItemEventReceiver : SPItemEventReceiver
    {
        #region Enumerations

        enum Assoc
        {
            Consultant,
            Permanent
        }

        #endregion Enumerations

        #region Methods

        /// <summary>
        /// An item is being deleted.
        /// </summary>
        public override void ItemDeleting( SPItemEventProperties properties )
        {
            if ( properties.ListItem.ContentType.Name.Equals( Recruiter.GetContentTypeName<Recruiter>( ) ) )
            {
                if ( doReferencesExistForRecruiter( properties ) )
                {
                    Recruiter recruiter = getRecruiter( properties );
                    properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                    properties.RedirectUrl = MessageHandler.GetRedirectUrl( MessageHandler.Message.Recruiter.RecruiterNotDeletedRecruiterIsReferenced, recruiter.Recruiter0 );
                }
            }
        }

        public override void ItemUpdated( SPItemEventProperties properties )
        {
            if ( properties.ListItem.ContentType.Name.Equals( Consultant.GetContentTypeName<Recruiter>( ) ) )
            {
                Recruiter recruiter = getRecruiter( properties );
                // if valid recruiter
                if ( recruiter != null )
                {
                    // get impacted consultants
                    List<Consultant> impacted = getConsultants( properties, recruiter );

                    // split into hire type (ie. consultant, permanent)
                    List<Consultant> consultants = impacted.FindAll( a => a.Type == Type.Consultant );
                    List<Consultant> permanents = impacted.FindAll( a => a.Type == Type.Permanent );

                    // remove calendar events associated with recruiter
                    CalendarEventCreator creator = new CalendarEventCreator( properties.WebUrl );
                    creator.RemoveCalendarEvents( recruiter );

                    List<Assoc> assoc = new List<Assoc>( );
                    if ( consultants.Count > 0 )
                        assoc.Add( Assoc.Consultant );
                    if ( permanents.Count > 0 )
                        assoc.Add( Assoc.Permanent );

                    foreach ( Assoc a in assoc )
                    {
                        // get appropriate activities based upon hire type (ie. consultant, permanent)
                        List<ConsultantCareActivity> activities = getActivities( properties, a );
                        // add calendar events
                        creator.AddCalendarEvents( a == Assoc.Consultant ? consultants : permanents, activities );
                    }
                }
            }
        }

        bool doReferencesExistForRecruiter( SPItemEventProperties properties )
        {
            ConsultantActivityIDDao dao = new ConsultantActivityIDDao( properties.WebUrl );
            return dao.ReferencesExistForRecruiter( properties.ListItem.UniqueId );
        }

        List<ConsultantCareActivity> getActivities( SPItemEventProperties properties, Assoc associated )
        {
            ConsultantCareActivityDao dao = new ConsultantCareActivityDao( properties.WebUrl );
            List<ConsultantCareActivity> activities = associated == Assoc.Consultant ? dao.GetAllConsultantAssociated( ) : dao.GetAllPermanentAssociated( );
            return activities;
        }

        List<Consultant> getConsultants( SPItemEventProperties properties, Recruiter recruiter )
        {
            ConsultantActivityIDDao dao = new ConsultantActivityIDDao( properties.WebUrl );
            return dao.GetConsultantsByRecruiter( recruiter.UniqueId );
        }

        Recruiter getRecruiter( SPItemEventProperties properties )
        {
            RecruiterDao dao = new RecruiterDao( properties.WebUrl );
            return dao.Get( properties.ListItem.UniqueId );
        }

        #endregion Methods
    }
}